package com.lzs.Controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Family;
import com.lzs.PO.Member;
import com.lzs.PO.Page;
import com.lzs.PO.Partdetail;
import com.lzs.PO.Standart;
import com.lzs.Service.FamilyService;
import com.lzs.Service.MemberService;
import com.lzs.Service.PartdetailService;
import com.lzs.Service.StandartService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/part")
public class PartController {
	@Autowired
	FamilyService familyService;
	@Autowired
	MemberService memberService;
	@Autowired
	PartdetailService partdetailService;
	@Autowired
	StandartService standartService;
	
	@RequestMapping(value = "/part_list")
	public String familyList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Family> page = this.familyService.findFamilyPagelist(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/part/part_list";

	}
     
	@RequestMapping(value = "/nopart_list")
	public String findMemberWithNoPart(String family_id, Model model) {
		
		List<Member> memberlistWithPart = new ArrayList<Member>();

		
		List<Member> memberlist = this.memberService.findMembersByFamilyId(family_id);

		for (int i = 0; i < memberlist.size(); i++) {

			if (this.partdetailService.findPartdetailByRuralId(memberlist.get(i).getRural_id()) != null) {
				memberlistWithPart.add(this.partdetailService.findPartdetailByRuralId(memberlist.get(i).getRural_id()));
			}

		}
		System.out.println("已经参合农民："+memberlistWithPart);

        memberlist.removeAll(memberlistWithPart);
        
		System.out.println("未参合农民："+memberlist);

		model.addAttribute("memberlist", memberlist);

		return "member/nopart_member_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/part_take")
	public Msg takePartIn(String[] rural_ids,String actor) {
		
		try {
			//获取系统当前时间

			SimpleDateFormat currentTime = new SimpleDateFormat("yyyyMMddHHmmss");
			
			SimpleDateFormat currentYear = new SimpleDateFormat("yyyy");
			
			//缴费年度
			
			String year = currentYear.format(new java.util.Date());
		
			//参合时间
			
			String part_time = currentTime.format(new java.util.Date());
			
			String part_ticket_id;
			
			String part_id;
			
			Date date=new Date();
			
			long timestamp=date.getTime();
			
			SimpleDateFormat format =  new SimpleDateFormat("yyyyMMddHHmmss");
			
			//参合登记编号
			
			part_id="T"+format.format(timestamp);     
			
			//参合发票号
			
			part_ticket_id="D"+format.format(timestamp); 
			
			//查询当年缴费标准
			
			Standart standart = this.standartService.findStandartByYear(year);
			
			//缴费费用
			
			double part_money = rural_ids.length*standart.getProportion();
			
			//缴费人数
			
			Integer part_size = rural_ids.length;
		    
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("part_ticket_id", part_ticket_id);
			
			map.put("part_money", part_money);
			
			map.put("part_size", part_size);
			
			map.put("part_id", part_id);
			
			map.put("part_time", part_time);
			
			map.put("rural_ids", rural_ids);
			
			map.put("year", year);
			
			map.put("actor", actor);
			
			this.partdetailService.addPart(map);
			
			this.partdetailService.addPartdetail(map);
			
			return Msg.success().add("part_money", part_money);
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
		
	}
}
